package cn.sdut.thrift.server;

import cn.sdut.thrift.HelloWorldService;
import cn.sdut.thrift.service.HelloWorldImpl;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;

/**
 * @author MaJiatao
 * @description: 使用标准的阻塞式IO
 * @created: 2019/10/22
 */
public class HelloServerDemoWithIO {
    public static final int SERVER_PORT = 8090;

    public void startServer() {
        try {
            System.out.println("HelloWorld TThreadPoolServer start");
            HelloWorldService.Processor<HelloWorldService.Iface> ifaceProcessor = new HelloWorldService.Processor<>(new HelloWorldImpl());
            TServerSocket tServerSocket = new TServerSocket(SERVER_PORT);
            TThreadPoolServer.Args args = new TThreadPoolServer.Args(tServerSocket);
            args.processor(ifaceProcessor);
            args.protocolFactory(new TBinaryProtocol.Factory());
            TThreadPoolServer tThreadPoolServer = new TThreadPoolServer(args);
            tThreadPoolServer.serve();
        }catch (Exception e) {
            System.out.println("thrift server start error!!!");
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        HelloServerDemoWithIO server = new HelloServerDemoWithIO();
        server.startServer();
    }
}
